package com.yjdb.sanyuantang.aop;

import cn.dev33.satoken.stp.StpUtil;
import com.yjdb.sanyuantang.common.entity.SysAdmin;
import com.yjdb.sanyuantang.common.entity.SysJournal;
import com.yjdb.sanyuantang.common.mapper.SysAdminMapper;
import com.yjdb.sanyuantang.common.mapper.SysJournalMapper;
import com.yjdb.sanyuantang.common.result.CommonResult;
import com.yjdb.sanyuantang.utils.IpUntil;
import jakarta.annotation.Resource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

import jakarta.servlet.http.HttpServletRequest;

import java.net.http.HttpRequest;
import java.util.Date;

/**
 * @ClassName OperationLogAspect
 * @Description 操作日志切面处理类
 * @Author 混沌酱
 * @Date 2023-06-25 16:47
 * @Version 1.0
 **/
@Aspect
@Component
public class OperationLogAspect {

    @Resource
    private SysJournalMapper sysJournalMapper;

    @Resource
    private SysAdminMapper sysAdminMapper;

    @Resource
    private HttpServletRequest request;

    private static final Logger logger = LoggerFactory.getLogger(OperationLogAspect.class);

    /**
     * 设置操作日志切入点   在注解的位置切入代码
     */
    @Pointcut("@annotation(com.yjdb.sanyuantang.aop.OperationLogAnnotation)")
    public void operLogPoinCut() {
    }

    @AfterReturning(returning  /**
     * 记录操作日志
     * @param joinPoint 方法的执行点
     * @param result  方法返回值
     * @throws Throwable
     */ = "result", value = "operLogPoinCut()")
    public void saveOperLog(JoinPoint joinPoint, Object result) throws Throwable {
        try {
            SysJournal sysJournal = new SysJournal();
            // 获取当前时间
            Date date = new Date();
            // 添加操作时间
            sysJournal.setCreateTime(date);
            // 从切面织入点处通过反射机制获取织入点处的方法
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            //获取切入点所在的方法
            Method method = signature.getMethod();
            //获取操作
            OperationLogAnnotation annotation = method.getAnnotation(OperationLogAnnotation.class);
            if (annotation != null) {
                sysJournal.setModule(annotation.operModule());
                sysJournal.setFunction(annotation.operFunction());
            }
            // 获取ip地址
            sysJournal.setIp(IpUntil.getIpAddr(request));
            sysJournal.setDescription(((CommonResult<?>) result).getMessage());
            // 获取登录账号信息
            String id = StpUtil.getLoginId().toString();
            SysAdmin sysAdmin = sysAdminMapper.selectById(id);
            sysJournal.setNickname(sysAdmin.getNickname());
            sysJournal.setPhone(sysAdmin.getPhone());
            sysJournalMapper.insert(sysJournal);
        } catch (Exception e) {
            logger.error("保存操作日志异常", e);
        }
    }
}
